package com.dream.inspect.modules.business.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dream.inspect.common.utils.R;
import com.dream.inspect.modules.business.service.AlwaysUseService;
import com.dream.inspect.modules.dream.pojo.SysMenuFormMap;
import com.dream.inspect.modules.sys.controller.AbstractController;
import com.dream.inspect.modules.sys.service.SysMenuService;



/**
 * 常用功能
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-06-16 17:04:56
 */
@RestController
@CrossOrigin
@RequestMapping("business/alwaysuse")
public class AlwaysUseController extends AbstractController{
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private AlwaysUseService alwaysUseService;

    /**
     * 选择列表
     */
    @RequestMapping("/useMenuChoseList")
    public R useMenuChoseList(){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("userId", getUserId());
    	params.put("roleId", getSessionUser().get("roleId"));
    	List<SysMenuFormMap> data = sysMenuService.getRoleMenuSelectUserUse(params);
        return R.ok().put("data", data);
    }
    
    /**
     * 保存常用功能
     * @param params
     * @return
     */
    @RequestMapping("/saveData")
    public R saveData(@RequestParam Map<String, Object> params){
    	try {
    		if(!params.containsKey("menuIds")){
    			params.put("menuIds", "");
    		}
    		params.put("userId", getUserId());
			alwaysUseService.insertEntity(params);
		} catch (Exception e) {
			logger.error("save alwaysUse error : " + e);
			return R.error("未知异常，请联系管理员").put("data", "未知异常，请联系管理员");
		}
    	return R.ok();
    }
    
    /**
     * 常用功能
     * @return
     */
    @RequestMapping("/useMenu")
    public R useMenu(){
    	Map<String, Object> params = new HashMap<String, Object>();
    	params.put("userId", getUserId());
    	List<SysMenuFormMap> data = sysMenuService.getUseMenu(params);
        return R.ok().put("data", data);
    }
}
